package com.todoit.wavebot.server;

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.todoit.wavebot.util.TodoitTaskList;


@SuppressWarnings("serial")
public class ExportServiceImpl extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        UserService userService = UserServiceFactory.getUserService();

        String thisURL = request.getRequestURI();
        
        TodoitTaskList dummyTaskList = new TodoitTaskList();
        
        if (request.getUserPrincipal() != null) {
        	// we have some identification
        	response.setContentType("text/csv"); // see below on why do this here.
        	// is the user in our database?
        	if (true) // TODO: waiting for integration
        	{
        		response.getWriter().println(dummyTaskList.toCSV());
        	}
        	else
        	{
            	// ideally we'd generate an error message
            	// but we might be queried by automated tools.
            	// so we'll have to return blank...
        		response.getWriter().println("");
        	}
        } else {
            // we're missing identification.
        	// lets hope a querying tool would be smart enough
        	// to understand that he got a broken response
        	response.setContentType("text/html"); 
            response.getWriter().println("<p>Please <a href=\"" +
                                         userService.createLoginURL(thisURL) +
                                         "\">sign in</a>.</p>");
        }
    }
}